
function testGridColumn$Name() {
	var fac = new zxt.ui.InteractorFactory();	
	var col = new zxt.ui.GridColumn("myName", "myTitle", 100, "myType", fac);
	
	assertEquals("", "myName", col.name());
	col.name("newName");
	assertEquals("", "newName", col.name());
}


function testGridColumn$Title() {
	var fac = new zxt.ui.InteractorFactory();	
	var col = new zxt.ui.GridColumn("myName", "myTitle", 100, "myType", fac);
	
	assertEquals("", "myTitle", col.title());
	col.title("newTitle");
	assertEquals("", "newTitle", col.title());
}


function testGridColumn$Width() {
	var fac = new zxt.ui.InteractorFactory();	
	var col = new zxt.ui.GridColumn("myName", "myTitle", 100, "myType", fac);
	
	assertEquals("", 100, col.width());
	col.width(111);
	assertEquals("", 111, col.width());
}

function testGridColumn$Type() {
	var fac = new zxt.ui.InteractorFactory();	
	var col = new zxt.ui.GridColumn("myName", "myTitle", 100, "myType", fac);
	
	assertEquals("", "myType", col.type());
	col.type("newType");
	assertEquals("", "newType", col.type());
}

function testGridColumn$Readonly() {
	var fac = new zxt.ui.InteractorFactory();	
	var col = new zxt.ui.GridColumn("myName", "myTitle", 100, "myType", fac);
	
	assertEquals("", true, col.readonly());
	col.readonly(false);
	assertEquals("", false, col.readonly());
}	

function testGridColumn$InteractorFac() {
	var fac = new zxt.ui.InteractorFactory();	
	var col = new zxt.ui.GridColumn("myName", "myTitle", 100, "myType", fac);
	
	assertEquals("", fac, col.interactorFac());
	
	var fac2 = new zxt.ui.InteractorFactory();	
	col.interactorFac(fac2);
	assertEquals("", fac2, col.interactorFac());
}	
	


function testGridBase$Start_Limit(){	
	var grid = new zxt.ui.GridBase(tmodel, true);
	
	assertEquals("", 0, grid.start());
	assertEquals("", 0, grid.limit());
	
	grid.start(11);
	grid.limit(101);

	assertEquals("", 11, grid.start());
	assertEquals("", 101, grid.limit());
	
}	

function testGridBase$GetColumns_GetColulmn(){
	var grid = new zxt.ui.GridBase(tmodel, true);
	var cols = grid.getColumns();
	
	assertEquals("", 5, cols.length);
	
	var colId = grid.getColumn("id");
	assertNotNull(colId);
	
	var colName = grid.getColumn("name");
	assertNotNull(colName);
	
	var colSex = grid.getColumn("sex");
	assertNotNull(colSex);
	
	var colBirth = grid.getColumn("birthday");
	assertNotNull(colBirth);
	
	var colIp = grid.getColumn("ip");
	assertNotNull(colIp);
		
	var colTtt = grid.getColumn("ttt");
	assertNull(colTtt);
	
}	

function testGridBase$AddColumn(){
	var grid = new zxt.ui.GridBase(tmodel, true);
	
	var cols = grid.getColumns();	
	assertEquals("", 5, cols.length);
	
	
	var renderD = function(grid, idx){ return "DETAIL"; };
	grid.addColumn( { name: "detail", title: "TTT", width: 140, render: renderD } );
	
	assertEquals("", 6, cols.length);
	assertEquals("", "detail", cols[5].name());

	var detailIF = cols[5].interactorFac();
	assertTrue("", detailIF instanceof zxt.ui.GridInteractorFactory);

	
	var fac = new zxt.ui.InteractorFactory();	
	grid.addColumn( { pos: 0, name: "ext2", title: "EXTENT", width: 140, interactorFac: fac } );
	assertEquals("", 7, cols.length);
	assertEquals("", "ext2", cols[0].name());
	assertEquals("", "detail", cols[6].name());
	
	
	var detailIF = cols[0].interactorFac();
	assertEquals("", fac, detailIF);
	
	
	grid.addColumn( { name: "ext3", title: "EXTENT3", width: 140, type: "text" } );
	assertEquals("", 8, cols.length);
	assertEquals("", "ext3", cols[7].name());
	
	var ext3IF = cols[7].interactorFac();
	assertTrue("", ext3IF instanceof zxt.ui.TextboxFactory);
	
	
}	


function testGridRow(){
	var grid = new zxt.ui.GridBase(tmodel, true);
	var row = new zxt.ui.GridRow(grid, 2);
	
	assertEquals("", 2, row.index());
	
	row.state("myState");
	assertEquals("", "myState", row.state());
	
}	

function testGridRow$Render(){
	
	var grid = new zxt.ui.GridBase(tmodel, true);
	var row = new zxt.ui.GridRow(grid, 2);
	
	row.setValue({id:"179371", name:"Wang Yongchun", sex:"Female", birthday:"1978-11-12", ip: "127.0.0.1" });
	
	var dom  = row.render();
	
	var clicked = false;
	grid.rowClicked = function(_dom, _idx){
		assertEquals("", 2, _idx);
		assertEquals("", dom, _dom);
		clicked = true;
	}	
	$(dom).click();
	assertTrue("", clicked);
	
	var dbclicked = false;
	grid.rowDoubleClicked = function(_dom, _idx){
		assertEquals("", 2, _idx);
		assertEquals("", dom, _dom);
		dbclicked = true;
	}	
	$(dom).dblclick();
	assertTrue("", dbclicked);
	
	var hoverover = false;
	grid.rowHover = function(_dom, _idx){
		assertEquals("", 2, _idx);
		assertEquals("", dom, _dom);
		hoverover = true;
	}	
	grid.rowHoverOut = function(_dom, _idx){
		assertEquals("", 2, _idx);
		assertEquals("", dom, _dom);
		hoverover = false;
	}
		
	$(dom).mouseover();
	assertTrue("", hoverover);
	$(dom).mouseout();
	assertFalse("", hoverover);
	
	
	var tds = $(">td", dom );
	assertEquals("", 6, tds.length);
	tds.each(
		function( idx, tddom ){
			var col = $(tddom).attr("column");
			
			if ( col == undefined ) return;
			
			var handled = false;
			grid._cellClicked = function(row, td){
				handled = true;
				var _col = $(td).attr("column");
				assertEquals("", col, _col);
			}
			
			$(tddom).click();
			assertTrue(handled);
		}
	);
		
}	

function testGridBase$GetColumnWidth_SetColumnWidth(){
	
	var grid = new zxt.ui.GridBase(tmodel, true);
	grid.render();
	
	try{
		var width = grid.getColumnWidth("nnn");
		fail("exception");
	}catch(e){
	}		
	
	width = grid.getColumnWidth("id");
	assertEquals("", 100, width);
	
	grid.setColumnWidth("id", 119);
	width = grid.getColumnWidth("id");
	assertEquals("", 119, width);
}

function testGridBase$ScrollTo(){
		
	var grid = new zxt.ui.GridBase(tmodel, true);
	grid.render();
	
	var refreshed = false;
	grid.refresh = function(){ refreshed = true; };
	
	assertEquals("", 0, grid.start());
	assertEquals("", 0, grid.limit());
	
	grid.scrollTo(11, 12);
	
	assertEquals("", 11, grid.start());
	assertEquals("", 12, grid.limit());
	assertEquals("", true, refreshed);
}	

function testGridBase$GetRowCount(){
	var grid = new zxt.ui.GridBase(tmodel, true);
	grid.render();
	
	assertEquals("", 0, grid.getRowCount());
	
	grid.scrollTo(11, 12);
	assertEquals("", 12, grid.getRowCount());
	
	grid.scrollTo(50, 10);
	assertEquals("", 8, grid.getRowCount());
}	


function testGridBase$GetRowOld(){
	var grid = new zxt.ui.GridBase(tmodel, true);
	grid.render();	
	grid.scrollTo(1, 10);
	
	var data = grid.getRowOld(1);
	assertEquals("", "180001", data.id);
	assertEquals("", "Zhou Laijun", data.name);	
	
}	

function testGridBase$GetRowData(){
	var grid = new zxt.ui.GridBase(tmodel, true);
	grid.render();	
	grid.scrollTo(1, 10);
	
	var row = grid._getRow(1);
	var nameI = row.getInteractor("name");
	
	nameI.setValue("JOHN WOO");
	nameI.render();
	
	var data = grid.getRowData(1);
	assertEquals("", "JOHN WOO", data.name);	
}	


function testDataGrid(){	
	var dg = new zxt.ui.DataGrid(tmodel, {selMode  : 1,	autoDraw : false});	
	assertEquals("", 3, dg.editMode());
	assertEquals("", 1, dg.selMode());
	assertEquals("", false, dg.autoDraw());	
	
	var cols = dg.getColumns();
	assertEquals("", 1, cols.length);
	
	var col = cols[0];
	assertEquals("", "$check", col.name());
	assertEquals("", "checkbox", col.type());
	
	
}

function testDataGrid$SetRowChecked(){
	var grid = new zxt.ui.DataGrid(tmodel, {editMode:1});
	grid.start(0);
	grid.limit(10);
	grid.render();
	
	var row = grid._getRow(0);
	var ckI = row.getInteractor("$check");
	assertFalse( ckI.getValue() ); 
	
	grid.setRowChecked(0, true);			
	assertTrue( ckI.getValue() ); 
	
	grid.setRowChecked(0, false);			
	assertFalse(ckI.getValue() ); 	
}	

function testDataGrid$SetRowChecked01(){
	var grid = new zxt.ui.DataGrid(tmodel, {editMode:1});
	grid.start(0);
	grid.limit(10);
	grid.render();
	
	var row = grid._getRow(0);		
	row.state($zxt.Mode.CRET);
	
	var messaged = false;
	row.onMessage = function(msg){
		messaged = true;
		assertEquals("", $zxt.MESSAGE.WM_MODE_CHANGE, msg.type );
		assertEquals("", $zxt.Mode.CRET, msg.data.mode );
	}		
	
	grid.setRowChecked(0, true);	
	assertTrue("message", messaged);
	
	messaged = false;
	row.onMessage = function(msg){
		messaged = true;
	}
	
	grid.setRowChecked(0, false);			
	assertFalse("message 2", messaged);	
}

function testDataGrid$SetRowChecked02(){
	var grid = new zxt.ui.DataGrid(tmodel, {editMode:2});
	grid.start(0);
	grid.limit(10);
	grid.render();
	
	var row = grid._getRow(0);		

	var messaged = false;
	row.onMessage = function(msg){
		messaged = true;
		assertEquals("", $zxt.MESSAGE.WM_MODE_CHANGE, msg.type );
		assertEquals("", $zxt.Mode.EDIT, msg.data.mode );
	}		
	
	grid.setRowChecked(0, true);	
	assertTrue("message", messaged);
	
	messaged = false;
	row.onMessage = function(msg){
		messaged = true;
		assertEquals("", $zxt.MESSAGE.WM_MODE_CHANGE, msg.type );
		assertEquals("", $zxt.Mode.VIEW, msg.data.mode );
	}
	
	grid.setRowChecked(0, false);			
	assertTrue("message 2", messaged);	
	
	
	var row = grid._getRow(1);		
	row.state($zxt.Mode.CRET);

	var messaged = false;
	row.onMessage = function(msg){
		messaged = true;
		assertEquals("", $zxt.MESSAGE.WM_MODE_CHANGE, msg.type );
		assertEquals("", $zxt.Mode.CRET, msg.data.mode );
	}		
	
	grid.setRowChecked(1, true);	
	assertTrue("message", messaged);
	
	messaged = false;
	row.onMessage = function(msg){
		messaged = true;
		assertEquals("", $zxt.MESSAGE.WM_MODE_CHANGE, msg.type );
		assertEquals("", $zxt.Mode.VIEW, msg.data.mode );
	}
	
	grid.setRowChecked(1, false);			
	assertTrue("message 2", messaged);	
	
	
}	

function testDataGrid$SetRowChecked03(){
	var grid = new zxt.ui.DataGrid(tmodel, {editMode:3});
	grid.start(0);
	grid.limit(10);
	grid.render();
	
	var row = grid._getRow(0);		

	var messaged = false;
	row.onMessage = function(msg){
		messaged = true;
	}		
	
	grid.setRowChecked(0, true);	
	assertFalse("message", messaged);
	
	messaged = false;
	row.onMessage = function(msg){
		messaged = true;
		assertEquals("", $zxt.MESSAGE.WM_MODE_CHANGE, msg.type );
		assertEquals("", $zxt.Mode.VIEW, msg.data.mode );
	}
	
	grid.setRowChecked(0, false);			
	assertTrue("message 2", messaged);	
		
}		

function testDataGrid$OnMessage(){
	
	var dg = new zxt.ui.DataGrid(tmodel, {});	
	
	var created = false;
	var deleted = false;
	var saved = false;
	
	dg._onCreate = function(){ created = true; };	
	dg._onDelete = function(){ deleted = true; };	
	dg._onSave = function(){ saved = true; };	
		
	var msg = new zxt.core.Message($zxt.MESSAGE.WM_OPERATION_INVOKE, {opt : $zxt.Operation.CREATE});	
	dg.onMessage(msg);	
	assertTrue("", created);
	
	var msg = new zxt.core.Message($zxt.MESSAGE.WM_OPERATION_INVOKE, {opt : $zxt.Operation.DELETE});	
	dg.onMessage(msg);	
	assertTrue("", deleted);

	var msg = new zxt.core.Message($zxt.MESSAGE.WM_OPERATION_INVOKE, {opt : $zxt.Operation.SAVE});	
	dg.onMessage(msg);	
	assertTrue("", saved);
	
}		


